package com.wxb.quartz.util;

import org.springframework.util.StringUtils;

import java.util.*;

public class RandomUtils {

    /**
     * getRandomSeed
     *
     * @return randomSeed
     */
    private static synchronized long getRandomSeed() {

        // 通过Math的random()函数返回一个double类型随机数，范围[0.0, 1.0)
        final double random = Math.random();

        // 通过d获取一个[0, 100000)之间的整数
        final int number = (int) (random * 100000);

        return number;
    }

    private static String getRandomStr(String seedStr, int length) {
        StringBuffer resultRandStr = new StringBuffer();
        int randNum = 0;
        Random rand = new Random(getRandomSeed());
        int randStrLen = seedStr.length();
        for (int i = 0; i < length; i++) {
            randNum = rand.nextInt(randStrLen);
            resultRandStr.append(seedStr.substring(randNum, randNum + 1));
        }
        return resultRandStr.toString();
    }

    public static String genRandomStr(String seedStr, int length) {
        return getRandomStr(seedStr, length);
    }

    /**
     * GenerateRandomStr
     *
     * @param length
     * @return
     */
    public static String generateRandomNumberStr(int length) {
        String seedStr = "0123456789";
        return getRandomStr(seedStr, length);
    }

    /**
     * GenerateRandomStr
     *
     * @param length
     * @return
     */
    public static String generateRandomStr(int length) {
        String seedStr = "ABCDEFGHIJKLMNOPQRSTUVWSYZabcdefghijklmnopqrstuvwsyz0123456789";
        return getRandomStr(seedStr, length);
    }

    /**
     * @param maxValue
     * @return
     * @method: generateRandomInt
     * @return: int
     * @author: WangShuDa
     */
    public static int generateRandomInt(int maxValue) {
        Random rand = new Random(getRandomSeed());
        return rand.nextInt(maxValue);
    }

    /**
     * replace with getStreamNo23 or getCorrelator32 please.
     * getStreamNo
     *
     * @return random(2bit) + yyyyMMddHHmmss + random(4bit)
     */
    @Deprecated
    public final static String getStreamNo20() {
        return generateTimeStampRandomStr(1, 2);
    }

    public final static String getStreamNo23() {
        return generateTimeStampRandomStr(2, 4);
    }

    /**
     * getCorrelator32
     *
     * @return random(2bit) + yyyyMMddHHmmss + random(2bit)
     */
    public static String getCorrelator32() {
        return generateTimeStampRandomStr(9, 6);
    }

    /**
     * @param randomLenBefore
     * @param randomLenAfter
     * @param timeFormat:     yyyyMMddHHmmssSSS
     * @return
     * @method: generateTimeStampRandomStr
     * @return: String
     * @author: WangShuDa
     */
    public static String generateTimeStampRandomStr(int randomLenBefore, int randomLenAfter, String timeFormat) {
        StringBuffer resultStr = new StringBuffer();
        resultStr.append(generateRandomStr(randomLenBefore));
        resultStr.append(DateUtils.getCurrentTimeString(timeFormat));
        resultStr.append(generateRandomStr(randomLenAfter));
        return resultStr.toString();
    }

    /**
     * @param randomLenBefore
     * @param randomLenAfter
     * @return generateTimeStampRandomStr(randomLenBefore, randomLenAfter, "yyyyMMddHHmmss");
     * @method: generateTimeStampRandomStr
     * @return: String
     * @author: WangShuDa
     */
    public static String generateTimeStampRandomStr(int randomLenBefore, int randomLenAfter) {
        return generateTimeStampRandomStr(randomLenBefore, randomLenAfter, "yyyyMMddHHmmssSSS");
    }

    public static String generateUuidStr() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static String genDataStreamNum() {
        return genDataStreamNum(null);
    }

    public final static String _DATA_STREAM_NUM_PREFIX = "S";

    public static String genDataStreamNum(String dynTblSuffix) {
        StringBuffer buffer = new StringBuffer(_DATA_STREAM_NUM_PREFIX);
        buffer.append(DateUtils.getCurrentTimeString());
        buffer.append("U");
        buffer.append(UUID.randomUUID().toString().replace("-", ""));
        //
        if (StringUtils.hasLength(dynTblSuffix)) {
            buffer.append("@").append(dynTblSuffix);
        }
        //
        return buffer.toString();
    }

    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
        List<String> arr = new ArrayList<>(10000);
        for (int index = 10000; index > 1; index--) {
            String s = getStreamNo23();
            System.out.println(s);
            arr.add(s);
        }
        System.out.println(System.currentTimeMillis());
        Set<String> set = new HashSet<>();
        set.addAll(arr);
        System.out.println(set.size());

    }

    public static String randomStreamNum() {
        StringBuffer stringBuffer = new StringBuffer(32);
        String result = stringBuffer.append("c").append(DateUtils.getCurrentTimeString())
                .append(RandomUtils.genRandomStr(RandomUtils.generateUuidStr(), 17)).toString();
        return result;
    }
}
